package com.cmccgz.udacs_landlord_webapp.data;


import android.util.Log;

import com.cmccgz.baselibrary.utils.BoxingUtils;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.IdBean;
import com.youtu.Youtu;

import org.json.JSONObject;

import java.io.File;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class IdScanDataRepository implements IdScanDataSource{
    //认证信息
    private String APP_ID = "10102888";
    private String SECRET_ID = "AKID1pSq0vO2FDwQfgQ1JL7duuDI8k7FomNq";
    private String SECRET_KEY = "PccMq42kBQRCixwiEVaPyOaXEfqlUxB9";

    private Youtu youtu;

    @Override
    public void scanId(final String filePath, final IdScanLisenter callBack) {
        //解析
        final String compressfilePath = BoxingUtils.compress(GlobalContext.getInstance(), filePath, 10 * 1024);
        Observable.just(compressfilePath)
                .subscribeOn(Schedulers.newThread())
                .map(new Func1<String, IdBean>() {
                    @Override
                    public IdBean call(String s) {
                        Log.e("inx", "call: s="+s);
                        Youtu youtu = getYoutu();
                        IdBean idBean=null;
                        try {
                            JSONObject jsonObject = youtu.IdCardOcr(s, 0);
                            Log.e("inx", "call: jsonObject="+jsonObject);
                            String name=jsonObject.optString("name");
                            String sex=jsonObject.getString("sex");
                            String nation=jsonObject.getString("nation");
                            String birth=jsonObject.getString("birth");
                            String id=jsonObject.getString("id");
                            String address=jsonObject.getString("address");
                            idBean=new IdBean(id,name,sex,birth,nation,address);
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e("inx", "call: e="+e);
                        }
                        Log.e("inx", "call: idBean="+idBean);
                        return idBean;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<IdBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.scanIdError("解析失败！请选择高清身份证正面照再次操作！");
                        delFile(filePath);
                        delFile(compressfilePath);
                    }

                    @Override
                    public void onNext(IdBean idBean) {
                        callBack.scanIdSuccess(idBean);
                        delFile(filePath);
                        delFile(compressfilePath);
                    }
                });
    }

    private Youtu getYoutu() {
        if (youtu == null) {
            youtu = new Youtu(APP_ID, SECRET_ID, SECRET_KEY, Youtu.API_YOUTU_END_POINT);
        }
        return youtu;
    }

    private boolean delFile(String filePath){
        File file = new File(filePath);
        boolean delete =false;
        if (file.exists()) {
            delete=file.delete();
        }
        return delete;
    }
}
